// pages/mine/mine.js
let that,
api = new(require('../../utils/api.js')),
qrcode = require('../../utils/qrcode.js'),
util = new(require('../../utils/util.js')),
  staticimg = api.getstaticimgurl(),
app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: "../pic/avatar.png",//用户头像。
    nickName: "点击登录",//用户昵称
    id:"",
    showshare:!1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    let env = wx.getAccountInfoSync();
    that.setData({
      showid: env.miniProgram.envVersion!='release'
    })
    console.log(env,'env')
    if (app.logininfo) {
     that.getuser()
    } else {
      util.setwatcher(app, this.afterlogin)
    }



    // let userInfo = wx.getStorageSync('userInfo'),user=wx.getStorageSync('user') ;
    // if(userInfo!=''){
    //   let {avatarUrl,nickName} = userInfo;
    //   that.setData({
    //     avatarUrl,nickName,haslogin:!0,id:user.id,usertype:user.type
    //   })
      
    // }else{


    // }
  },
  tomycar(){
    wx.navigateTo({
      url: '../mystore/mystore',
    })
  },

  share(){
    that.setData({ showshare: !0 })
    wx.showLoading({
      title: '加载中',
    })
    wx.getSystemInfo({
      success: (res) => {
        new qrcode({
          width: res.screenWidth * 0.65,
          height: res.screenWidth * 0.65,
          canvasId: 'canvas',
          text: 'https://chinawzhs.cn/share?id=' + that.data.id,
          callback: res => {
            wx.hideLoading();
          
          }
        })
      },
    })
   
  },
  tocar(){
  //  util.nocancelmodal('')
    wx.navigateTo({
      url: '../car/car', 
    })
  }, 

  closemodal(){
    that.setData({showshare:!1})
  },

  savepic(){
      wx.canvasToTempFilePath({
              canvasId: 'canvas',
              quality: 0,
              success: res => {
                wx.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: res1 => {
                    wx.showToast({
                      title: '保存成功',
                      icon: 'none'
                    })
                  },
                })


              }
            }, that)


  
  },

  afterlogin: {
    logininfo: res => {
      that.getuser()
    } 
  },
  getuser(){
    let info=app.logininfo
    if(info.user.isblock=='1'){
      wx.reLaunch({
        url: '../block/block',
      })
      // return;
    }
    wx.showLoading({
      title: '加载中',
    })
    let userInfo = wx.getStorageSync('userInfo'),user=wx.getStorageSync('user') ;
    if(userInfo!=''){
     
      let {avatarUrl,nickName} = userInfo;
      that.setData({
        avatarUrl,nickName,haslogin:!0,
      })
    }
    that.setData({id:user.id,usertype:user.type,isopenrecharge:user.isopenrecharge})
    wx.hideLoading()
  },
  // getuserinfo(e){
  //   console.log(e,'getuserinfo');
  //   let {userInfo} = e.detail,{avatarUrl,nickName} = userInfo,user=wx.getStorageSync('user');
  //   api.getnameurl({avatarUrl,nickName},res=>{
  //     if(res.data.issuccess==1){
  //       wx.setStorage({
  //         data: userInfo,
  //         key: 'userInfo',
  //       })
    
  //       that.setData({
  //         avatarUrl,nickName,haslogin:!0,id:user.id,
  //       })
  //     }else{
  //       wx.showModal({
  //         title: '登录失败',
  //         content: '请重新登录',
  //         success:function(res){
  //           if(res.confirm){
  //             that.getuserinfo(e)
             
  //           }
  //         },
  //       })
  //     }
  //   })
    
  // },
  getUserProfile(){
    wx.getUserProfile({
      desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        if (res.userInfo) {
           let userInfo = res.userInfo,{avatarUrl,nickName} = userInfo,user=wx.getStorageSync('user');
          api.getnameurl({avatarUrl,nickName},res=>{
            if(res.data.issuccess==1){
              wx.setStorage({
                data: userInfo,
                key: 'userInfo',
              })
          
              that.setData({
                avatarUrl,nickName,haslogin:!0,id:user.id,
              })
            }else{
              wx.showModal({
                title: '登录失败',
                content: '请重新登录',
                success:function(res){
                  if(res.confirm){
                    that.getuserinfo(e)
                   
                  }
                },
              })
            }
          })

        } else {
          console.log("点击了拒绝授权");
          // wx.navigateBack();
        }



        // this.setData({
        //   userInfo: res.userInfo,
        //   hasUserInfo: true
        // })
      },fail(res){
        console.log("点击了拒绝授权");
        wx.navigateBack();
      }
    })
  },
  toindex(){
    wx.switchTab({
      url: '../index/index'
    })
  },
  toorder(e){
    let {url}=e.currentTarget.dataset
    // console.log(e.currentTarget.dataset,'eeee');
    
    wx.navigateTo({
      url: `../${url}/${url}`,
    })
  },
  tomap(){
    let usertype = wx.getStorageSync('user').type;
    wx.navigateTo({
      url: '../aroundmap/aroundmap?usertype='+usertype,
    })
  },
  // gotap(e){
  //   console.log(e,'tap');
    
  //   let {path} = e.currentTarget.dataset;
  //   wx.navigateTo({
  //     url: `../${path}/${path}`,
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '手机扫一扫，废纸上门收',
      path: '/pages/index/index?shareid='+that.data.id,
      imageUrl: staticimg+"share.jpg"
    }
  }
})